// document.write("<script language=javascript :src= BASE_URL +'cdn/avue/DSWebsocketEngine.js'></script>");

function web_DSConnectionCheck()				{ConnectionTest();Invoke(arguments);}
function web_DSInitService()					{InitService("noble-print:7999");}
function web_DSisConnectOn() 					{return ds_isConnectOn(); }
function web_Wait(timeout, cb) 					{Sleep(timeout, cb); }
function web_DSQuitSvc()                        {Invoke(arguments);}

/////////////////////////////////


function web_DSEnumPrinter()            		{Invoke(arguments);}
function web_DSOpenPrinter()   					{Invoke(arguments);}
function web_DSGetPrinterModel() 				{Invoke(arguments);}
function web_DSClosePrinter() 					{Invoke(arguments);}
function web_DSGetPrinterSN() 					{Invoke(arguments);}
function web_DSPrintTraceInfo() 				{Invoke(arguments);}
function web_DSDownloadFile()                   {Invoke(arguments);}
function web_DSPrintIssuePermit()               {Invoke(arguments);}

function web_DSZPLPrintTextExA()                {Invoke(arguments);}
function web_DSZPLPrintCode128A()               {Invoke(arguments);}
function web_DSZPLPrintQrCodeExA()              {Invoke(arguments);}
function web_DSGetDeviceStatus()                {Invoke(arguments);}
function web_DSZPLRotation()                    {Invoke(arguments);}
function web_DSZPLSetBarcodeDefaults()          {Invoke(arguments);}
function web_DSZPLPrintBarCodeEx()              {Invoke(arguments);}
function web_DSZPLPrintLine()                   {Invoke(arguments);}
function web_DSZPLSetPageLength()               {Invoke(arguments);}

///////////////////////////////


//////////////////////////////////////
function web_DSZPLPrintImageExA_SizeScalable()  {Invoke(arguments);}
//////////////////////////////////////
function test_fun()                     		{Invoke("test_fun",1,2,3,[1,2],arguments[0]);}


function DSInitService()
{
	web_DSInitService();
}

function DSLaunchApp()
{
	web_DSLaunchApp();
}

function web_DSLaunchApp()
{
	var a =document.createElement("a");
    //a.href="dsdraw:none";
	a.href="a1:none";
	//a.href="www.baidu.com";
	//a.display="none";
    document.body.appendChild(a);
    a.click();

    var c = arguments.length;
    if (c > 0) {
        if (typeof arguments[0] == "object") {
            var cb = arguments[0];
            Sleep(5000, cb);
        }
    }
}

function web_DSNewLaunchApp()
{
	var e = document.createElement("a");
	var reg = RegExp("^[A-Za-z0-9]+(://){1}$");
	if(arguments[0] &&
		typeof arguments[0] == "string" && reg.test(arguments[0]))
		e.href = arguments[0];
	else e.href = "DSWebPrtSvc://";//alert("error");
	e.innerHTML="";
	document.getElementsByTagName("body")[0].appendChild(e);


	var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
	var myHref;
	if(isIE || isIE11){
		//window.history.back(-1);
		myHref = window.location.href;

	}
	e.click();
	if(isIE || isIE11){
		//window.history.back(-1);
		var textHref = window.location.href;
		if(myHref != window.location.href)
		window.history.back(-1);
	}

	document.getElementsByTagName("body")[0].removeChild(e);
}

function web_DSPrtEngineStartup1()
{
	DSInitService();
    web_Wait(1000, function (res) {
        if (!res) {
          console.log("服务未启动，请启动服务；");
            web_DSNewLaunchApp();
            web_Wait(15000, function (res) {
                if (!res)
                    alert("启动服务失败");
                else{
                  console.log("启动服务成功");
                }
            });
        }
        else{
          console.log("启动服务成功");
          web_DSClosePrinter(closeCallBackFun);
          web_DSEnumPrinter(getPrintcallBackFun);
        }

    });
}

function closeCallBackFun(retVal){
  console.log("关闭打印机："+retVal.RtnVal[0]);
}

function getPrintcallBackFun(retVal){
  console.log(retVal);
  if(retVal.RtnVal==null)
    return;
  if(retVal.RtnCode==0){
    //output(retVal.RtnVal[0]);
    //output(retVal.RtnVal[1]);
    for(var i in retVal.RtnVal){
      var ii=i+1;
      console.log("第"+ii+"台打印机："+retVal.RtnVal[i]);
      var temp=retVal.RtnVal[i];
      var printerName=temp.substring(temp.indexOf("@")+1);
      console.log(printerName)
      web_DSOpenPrinter(printerName,openCallBackFun);
      web_DSGetPrinterModel(getModelcallBackFun);

    }
  }
}
function getModelcallBackFun(retVal){
  console.log("打印机类型："+retVal.RtnVal);
}
function openCallBackFun(retVal){
  console.log("打开打印机："+retVal.RtnVal);
}

function web_DSPrtEngineStartup(callBackFun,ifOpen)
{
	if("function" != typeof arguments[0])return;
	DSInitService();

    web_Wait(1000, function (res) {
        if (!res) {
            callBackFun(-1);
            if(true == ifOpen)web_DSNewLaunchApp("DSWebPrtSvc://");
            web_Wait(15000,callBackFun);
        }
        else
            callBackFun(true);
    });
}
